import React from "react";
import Relay from "react-relay";
import DelQiniuFileMutation from "../../mutation/DelQiniuFileMutation" // 删除图片
import {Upload,Button,Icon,message} from 'antd';
import Config from "../../../../../../../util/Config"

export default class A extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            fileList:[]
        };


        this.url = Config.qiuniu;

        this.updataFile = this.updataFile.bind(this);
        this.onFilChange = this.onFilChange.bind(this);
        this.picRemove = this.picRemove.bind(this);
    }

    updataFile = (file)=>{
        const token = this.props.token,
               date = new Date().getTime();
        const obj = {file,token,key:`${(date+"").slice(-5)}_${file.name}`};
        return {file,token,key:`${(date+"").slice(-5)}_${file.name}`};

    }

    //
    onFilChange({file,fileList,event}){
        this.props.callBack(fileList);
        // 添加和删除都执行此操作，更新List
        if(file.status === "done"){
            !(function(){
                for(let i in fileList){
                    fileList[i].url = this.url + fileList[i].response.key + `?attname=${fileList[i].name}`
                }
                this.props.callBack(fileList);
            }.bind(this))();
        } else if(file.status == "error") {
            // this.props.getToken();
            message.warn('页面请求超时，请刷新页面');
        }

    }

    picRemove(file){
      if(file.response && file.response.key){
                Relay.Store.commitUpdate(
                    new DelQiniuFileMutation({
                        fileKey: file.response.key
                    }),
                    {
                        onFailure: () => {
                        },
                        onSuccess: (response) => {
                            let errorCode = response.delQiniuFile.delQiniuFile.errorCode;
                            if (errorCode == 0) {}
                        }
                    }
                )

       }else{
          
       }
    }
    handleClickSelf(){
        this.props.handleClickSelf();
    }
    render() {
      const {filePlugin,className,fileList,defaultList} = this.props;
      let {isShowTrigger} = this.props;

        if(isShowTrigger === undefined) { isShowTrigger = true; }

        return isShowTrigger ? (
            <Upload
                className={className}
                {...filePlugin}
                fileList={fileList}
                data={this.updataFile}
                onChange={this.onFilChange}
                onRemove={this.picRemove}
                defaultList={defaultList}
            >
                <Button>
                    <Icon type="upload" /> 上传附件
                </Button>
            </Upload>
        ):(
            <Upload
                className={className}
                {...filePlugin}
                fileList={fileList}
                data={this.updataFile}
                onChange={this.onFilChange}
                onRemove={this.picRemove}
                defaultList={defaultList}
                disabled={true}
            >
                <span className="hbdjah" onClick={this.handleClickSelf.bind(this)}>
                   <Button>
                        <Icon type="upload" /> 上传附件
                    </Button>
                </span>
            </Upload>
        )
    }

}
